package com.gasolinator.a.app.helper;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import com.gasolinator.a.util.Constants;
import org.json.JSONException;

/**
 * Clase que permite leer las preferencias de la configuracion de Gasolinator.
 */
public class GasolinatorPreferences implements GasolinatorConfig {

    public final static String PREF_DEVICE_ID = "pref_dixio_deviceid";
    public final static String PREF_HIDE_NOTIFICATIONS = "pref_dkeepnot";
    public final static String PREF_ONLY_DEFINITIONS = "pref_dixioonlydefs";
    public final static String PREF_HTTP_GZIP = "pref_dixio_http_gzip";
    public final static String PREF_NUMBER_OF_RESPONSES = "pref_maxnumsearch";
    public final static String PREF_CONNECTION_STATE = "pref_connectionstate";
    public final static String PREF_LANGUAGE = "pref_dixiointerfacelanguage";

    public static final String PREF_CHOFERA_OTHER = "chofera_otras";
    public static final String PREF_CHOFERA_ALCAMPO = "chofera_alcampo";
    public static final String PREF_CHOFERA_BP = "chofera_bp";
    public static final String PREF_CHOFERA_CAMPSA = "chofera_campsa";
    public static final String PREF_CHOFERA_CARREFOUR = "chofera_carrefour";
    public static final String PREF_CHOFERA_CEPSA = "chofera_cepsa";
    public static final String PREF_CHOFERA_EROSKI = "chofera_eroski";
    public static final String PREF_CHOFERA_ESSO = "chofera_esso";
    public static final String PREF_CHOFERA_GALP = "chofera_galp";
    public static final String PREF_CHOFERA_MEROIL = "chofera_meroil";
    public static final String PREF_CHOFERA_PETRONOR = "chofera_petronor";
    public static final String PREF_CHOFERA_REPSOL = "chofera_repsol";
    public static final String PREF_CHOFERA_SHELL = "chofera_shell";


    private static final String PREF_FULLSCREEN = "fullscreen";
    private static final String PREF_IS_LOCAL_SEARCH = "local_search";

    public final static String PREF_LANGUAGE_EN = "en";

    public final static String PREF_LANGUAGE_ES = "es";
    public final static String PREF_LANGUAGE_DEFAUTL = PREF_LANGUAGE_EN;
    private SharedPreferences pref;
    private static final String PREF_COMBUSTIBLE = "combustible";
    private static final String PREF_LAST_JSON = "last_json";
    private static final String PREF_DISTANCES = "prefs_distance";
    private static final String PREF_USE_GPS = "prefs_use_gps";
    private static final String PREF_FAVORITES = "gasolinator_favorites";
    private static final String PREF_PROXIMITY_ALERT = "prefs_prox_alert";
    private static final String PREF_INIT_ON_START = "prefs_update_onload";
    private static final String PREF_SATELLITE = "prefs_satellite";

    public GasolinatorPreferences(Context ctx) {

        this(PreferenceManager.getDefaultSharedPreferences(ctx));
    }

    public GasolinatorPreferences(SharedPreferences pref) {
        this.pref = pref;
    }

    public boolean isHttpGzip() {
        if (BUILD_FOR_TEST) {
            return pref.getBoolean(GasolinatorPreferences.PREF_HTTP_GZIP, false);
        }
        else {
            return true;
        }
    }

    public boolean isNotificationHidden() {
        if (BUILD_FOR_TEST) {
            return pref.getBoolean(GasolinatorPreferences.PREF_HIDE_NOTIFICATIONS, true);
        }
        else {
            return true;
        }
    }

    public boolean isOnlyDefinitions() {
        return pref.getBoolean(GasolinatorPreferences.PREF_ONLY_DEFINITIONS, false);
    }

    public String getLanguage() {
        return pref.getString(GasolinatorPreferences.PREF_LANGUAGE, "en");
    }

    public void setLanguage(String lang) {
        SharedPreferences.Editor editor = pref.edit();

        editor.putString(GasolinatorPreferences.PREF_LANGUAGE, lang);
        editor.commit();
    }


    public Integer getNumberOfResponses() {
        return Integer.parseInt(pref.getString(GasolinatorPreferences.PREF_NUMBER_OF_RESPONSES, "3"));
    }

    public ConnectionState getConnectionState() {
        Integer dixioconnectionstate = (Integer) Integer.parseInt(pref.getString(GasolinatorPreferences.PREF_CONNECTION_STATE, "1"));
        String logMsg="connection state: "+dixioconnectionstate.toString();
        Log.i(Constants.PREF_TAG, logMsg);
        switch(dixioconnectionstate) {
            case 1: return ConnectionState.LOCAL;
            case 2: return ConnectionState.REMOTE;
            default: return ConnectionState.BOTH;
        }
    }

    public void setConnectionState(ConnectionState state) {
        SharedPreferences.Editor editor = pref.edit();
        String connectionStateValue = "1";

        if (state == ConnectionState.LOCAL) {
            connectionStateValue = "1";
        }
        else if (state == ConnectionState.REMOTE) {
            connectionStateValue = "2";
        }
        else {
            // BOTH
            connectionStateValue = "3";
        }
        editor.putString(GasolinatorPreferences.PREF_CONNECTION_STATE, connectionStateValue);
        editor.commit();
    }

    public ConnectionState convertConnectionState(String state) {
        String connectionStateValue = "1";

        if ("LOCAL".equals(state)) {
            return ConnectionState.LOCAL;
        }
        else if ("REMOTE".equals(state)) {
            return ConnectionState.REMOTE;
        }
        else if ("BOTH".equals(state)) {
            return ConnectionState.BOTH;
        }
        else {
            return null;
        }
    }

    public String getBrandLists() {
        StringBuilder builder = new StringBuilder();

        if (pref.getBoolean(GasolinatorPreferences.PREF_CHOFERA_ALCAMPO, true)) builder.append("alcampo ");
        if (pref.getBoolean(GasolinatorPreferences.PREF_CHOFERA_BP, true)) builder.append("bp ");
        if (pref.getBoolean(GasolinatorPreferences.PREF_CHOFERA_CAMPSA, true)) builder.append("campsa ");
        if (pref.getBoolean(GasolinatorPreferences.PREF_CHOFERA_CARREFOUR, true)) builder.append("carrefour ");
        if (pref.getBoolean(GasolinatorPreferences.PREF_CHOFERA_CEPSA, true)) builder.append("cepsa ");
        if (pref.getBoolean(GasolinatorPreferences.PREF_CHOFERA_EROSKI, true)) builder.append("eroski ");
        if (pref.getBoolean(GasolinatorPreferences.PREF_CHOFERA_ESSO, true)) builder.append("esso ");
        if (pref.getBoolean(GasolinatorPreferences.PREF_CHOFERA_GALP, true)) builder.append("galp ");
        if (pref.getBoolean(GasolinatorPreferences.PREF_CHOFERA_MEROIL, true)) builder.append("meroil ");
        if (pref.getBoolean(GasolinatorPreferences.PREF_CHOFERA_PETRONOR, true)) builder.append("petronor ");
        if (pref.getBoolean(GasolinatorPreferences.PREF_CHOFERA_REPSOL, true)) builder.append("repsol ");
        if (pref.getBoolean(GasolinatorPreferences.PREF_CHOFERA_SHELL, true)) builder.append("shell ");

        return builder.toString();

    }

    public boolean getOtherBrands() {
        return pref.getBoolean(GasolinatorPreferences.PREF_CHOFERA_OTHER, true);
    }

    public boolean isLocalSearch() {
        return pref.getBoolean(GasolinatorPreferences.PREF_IS_LOCAL_SEARCH, false);
    }

    public boolean getFullscreen() {
        return pref.getBoolean(GasolinatorPreferences.PREF_FULLSCREEN, false);
    }

    public String getCombustible() {
        return pref.getString(GasolinatorPreferences.PREF_COMBUSTIBLE, "gasoil");
    }

    public void setLastJson(String json) {
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(GasolinatorPreferences.PREF_LAST_JSON, json);
        editor.commit();
    }
    public String getLastJson() {
        return pref.getString(GasolinatorPreferences.PREF_LAST_JSON, "[]");
    }

    public int getRadius() {
        String radiusString = pref.getString(GasolinatorPreferences.PREF_DISTANCES, "2");
        return Integer.parseInt(radiusString);
    }

    public boolean getUseGps() {
        return  pref.getBoolean(GasolinatorPreferences.PREF_USE_GPS, false);
    }

    public void handleFavorites(Integer gasStationId, boolean active) {

        try {
            String favs = pref.getString(GasolinatorPreferences.PREF_FAVORITES, "[]");
            String jsonArrayString = JsonUtils.getUpdatedFavoritesJson(gasStationId, active, favs);

            setFavorites(jsonArrayString);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    private void setFavorites(String json) {
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(GasolinatorPreferences.PREF_FAVORITES, json);
        editor.commit();
    }

    public boolean getIsFavorite(Integer gasStationId) {
        int isFavorite = 0;
        try {
            String favs = pref.getString(GasolinatorPreferences.PREF_FAVORITES, "[]");
            isFavorite = JsonUtils.isGasIdInFavorites(gasStationId, favs);
        } catch (JSONException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return (isFavorite == 0)  ? false : true;
    }

    public boolean getProximityAlert() {
        return pref.getBoolean(GasolinatorPreferences.PREF_PROXIMITY_ALERT, false);

    }

    public boolean getUpdateOnStart() {
        return pref.getBoolean(GasolinatorPreferences.PREF_INIT_ON_START, true);
    }

    public boolean getSatellite() {
        return pref.getBoolean(GasolinatorPreferences.PREF_SATELLITE, true);


    }
}
